<?php
namespace app\middleware;

use app\utils\JwtService;

class JwtAuth
{
    public function handle($request, \Closure $next)
    {
        $token = $request->header('Authorization');

        if (!$token) {
            return json(['code' => 401, 'msg' => '请先登录']);
        }

        try {
            $payload = JwtService::verifyToken($token);
            // 注入用户ID到 request
            $request->userId = $payload['uid'];
        } catch (\Exception $e) {
            return json(['code' => 401, 'msg' => $e->getMessage()]);
        }

        return $next($request);
    }
}
